﻿using UnityEngine;
using System.Collections.Generic;
using TowerDefence_Study;
using WuLibrary;

namespace TowerDefence
{
    public class BattleManager
    {
        // public static int BattleEnemyCount;

        private static float mTime;

        private static List<EnemyInfo> CallEnemyList = new List<EnemyInfo>();
        
        public static List<Vector3> MapPathList;

        public static int Homelife;

        private static UI_Battle UIBattle;

        public static List<Enemy> mEnemyList = new List<Enemy>();
        
        public static List<Tower> mTowerList = new List<Tower>();

        public static bool mBattleStop = false;

        public static int CurrentWave;
        public static int WaveTime;

       
        public static void InitData(UI_Battle ui,List<Vector3> mapPathList)
        {
            UIBattle = ui;
            // BattleEnemyCount = enemyCount;
            MapPathList = mapPathList;

         
            
            CallEnemyList.Add(new EnemyInfo()
            {
                ModelName  ="Warrior",
                Hp = 10,
                MoveSpeed = 1,
                Count = 5,
                Wave = 1
            });
            
            CallEnemyList.Add(new EnemyInfo()
            {
                ModelName  ="Archer",
                Hp = 5,
                MoveSpeed = 1.5f,
                Count = 3,
                Wave = 2
            });
            CurrentWave = 0;
            mTime = 0;
            Homelife = 5;
            mBattleStop=false;
            for (int i = 0; i < mEnemyList.Count-1; i++)
            {
                mEnemyList[i].DeleteObj();
            }
            mEnemyList.Clear();
            
            
        }

        public static void BattleUpdate()
        {
            if (mBattleStop == false)
            {
                if (CallEnemyList.Count > 0)
                {
                    mTime += Time.deltaTime;
                    if (mTime >= 1)
                    {
                        mTime -= 1;
                        if (WaveTime > 0)
                        {
                            
                                WaveTime -= 1;
                                UIBattle.RefershWaveTime(WaveTime);
                            
                           
                            
                        }
                        else if (CurrentWave < CallEnemyList[0].Wave)
                        {
                            
                                CurrentWave++;
                                WaveTime = 5;
                                UIBattle.RefershWaveTime(WaveTime);
                                UIBattle.RefershWave(CurrentWave);
                            
                            
                           
                        }
                        else
                        {
                            Enemy enemy= EnemyManager.CreateEnemy(CallEnemyList[0],
                                MapPathList);
                            UIBattle.BindEnemy(enemy);
                            // BattleEnemyCount--;
                            mEnemyList.Add(enemy);
                            if (CallEnemyList[0].Count > 1)
                            {
                                CallEnemyList[0].Count--;
                            }
                            else
                            {
                                CallEnemyList.RemoveAt(0);
                            }
                        }
                        
                    }
                }

                for (int i = mEnemyList.Count-1; i >=0; i--)
                {
                    if (mEnemyList[i] == null)
                    {
                        mEnemyList.RemoveAt(i);
                    }
                    else
                    {
                        mEnemyList[i].BattleUpdate();
                        if (mEnemyList[i] .mHP<=0)
                        {
                            mEnemyList.RemoveAt(i);
                        }
                    }
                }

                if (Homelife>0&&CallEnemyList.Count == 0 && mEnemyList.Count == 0)
                {
                    mBattleStop = true;
                    UIManager.EnterUI<UIBattleResult>().InitData(true);
                    UIBattle.BattleStop();
                    ColorDebugTools.Log("战斗胜利",LogColor.Red);
                }
            }
            
        }

        public static void EnemyAttack()
        {
            if (Homelife > 1)
            {
                Homelife--;
            }
            else
            {
                Homelife--;
                mBattleStop = true;
                UIBattle.BattleStop();
                UIManager.EnterUI<UIBattleResult>().InitData(false);
                ColorDebugTools.Log("战斗失败",LogColor.Red);
            }
            UIBattle.RefershLife();
        }

        public static void Clear()
        {
            MapManager.ClearMapBox();
            for (int i = 0; i < mEnemyList.Count; i++)
            {
                GameObject.Destroy(mEnemyList[i].gameObject);
            }
            mEnemyList.Clear();

            for (int i = 0; i < mTowerList.Count; i++)
            {
                GameObject.Destroy(mTowerList[i].gameObject);
            }
            mTowerList.Clear();
        }

        public static void CreatTower(Vector3 pos,TowerInfo info)
        {
            GameObject tower= TowerManager.CreateTower(info.MOdelName);
            tower.transform.localPosition = pos;
            Tower to= tower.AddComponent<Tower>();
            to.InitData(info);
            mTowerList.Add(to);
        }

        public static bool IsClickTower(Vector3 pos)
        {
            //是否点击防御塔
            for (int i = 0; i < mTowerList.Count; i++)
            {
                if (pos == mTowerList[i].transform.localPosition)
                {
                    return true;
                }
            }

            for (int i = 0; i < MapManager.mMapRunBoxList.Count; i++)
            {
                if (Vector3.Distance(pos,MapManager.mMapRunBoxList[i].transform.localPosition)<0.1f)  
                {
                    return true;
                }
            }
            
            return false;
        }
    }
}